package com.ruoyi.maode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.maode.domain.dto.AddButtonDTO;

import com.ruoyi.maode.domain.dto.QueryButtonDTO;
import com.ruoyi.maode.domain.entity.PartBpmnButton;
import com.ruoyi.maode.domain.vo.ButtonVO;
import com.ruoyi.maode.service.PartBpmnButtonService;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.ISysUserRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.core.config.ResultDTO;
import com.ruoyi.web.util.PageQuery;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 审核按钮Controller类
 * @author zhangwenqiang
 * @date 2025/09/22
 **/
@RestController
@RequestMapping("/button")
@Slf4j
public class PartBpmnButtonController {

    @Autowired
    private PartBpmnButtonService partBpmnButtonService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 新增按钮接口
     *
     * @author zhangwenqiang
     */
    @PostMapping("/add")
    public ResultDTO addButton(@RequestBody AddButtonDTO dto) {
        return ResultDTO.success(partBpmnButtonService.addButton(dto));
    }


    @DeleteMapping("/delete/{id}")
    public ResultDTO deleteButton(@PathVariable("id")Long id) {
        return ResultDTO.success(partBpmnButtonService.removeById(id));
    }

    @ApiOperation(value = "批量删除按钮")
    @DeleteMapping("/deleteByIds")
    public ResultDTO deleteByIds(@RequestBody List<Long> ids) {
        boolean b = partBpmnButtonService.removeBatchByIds(ids);
        return b ? ResultDTO.success() : ResultDTO.error();
    }

    @PutMapping("/edit")
    public ResultDTO editButton(@RequestBody AddButtonDTO dto) {
        int result = partBpmnButtonService.updateButton(dto);
        return result > 0 ? ResultDTO.success() : ResultDTO.error();
    }

    @PostMapping("/list")
    public ResultDTO<Page<ButtonVO>> list(@RequestBody PageQuery<QueryButtonDTO> dto) {
        return ResultDTO.success(partBpmnButtonService.list(dto));
    }

    /**
     * 获取按钮详情信息
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResultDTO getDetail(@PathVariable("id")Long id) {
        return ResultDTO.success(partBpmnButtonService.selectButton(id));
    }

    @GetMapping("/getApproveUser/{buttonId}")
    public ResultDTO getApproveUser(@PathVariable("buttonId")Integer buttonId) {
        return ResultDTO.success(partBpmnButtonService.getApproveUser(buttonId));
    }

    /**
     * 获取当前审批人的对应审批按钮
     * @return
     */
    @GetMapping("/getApproveSubmit")
    public ResultDTO getApproveSubmit() {
        Long userId = SecurityUtils.getUserId();
        SysRole role = sysRoleMapper.selectRoleByUserId(userId);
        // 根据当前角色获取对应的ButtonId
        return ResultDTO.success(partBpmnButtonService.getApproveSubmit(role.getRoleId()));
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("/getUser")
    public ResultDTO getUser() {
        return ResultDTO.success(sysUserService.selectUserList());
    }
}
